package user;

import jdbc.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 用户操作实现类
 * @author 彬彬
 */
public class UsersImpl implements UsersDAO{
    /**
     * 查询所有用户
     * @return
     */
    @Override
    public ResultSet findAll() {
        ResultSet rs = null;
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        Statement stmt = null;
        try {
//        获取数据库连接
            conn = jdbcUtils.ConnectMysql();
//        获取 Statement对象
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql = "select * from users";
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        while(true){
            try {
                if (!rs.next()) break;
                int id = rs.getInt("id");
                String username = rs.getString("name");
                String userpass = rs.getString("password");
                String phone = rs.getString("phone");
                System.out.println("id: " + id + "\tname: "+ username + "\tpass: "+ userpass + "\tphone: "+phone+"\n");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        System.out.println(rs);
        jdbcUtils.release(rs,stmt,conn);
        return rs;
    }

    /**
     * 用户登录操作
     * @param name 用户名
     * @param passwd 用户密码
     * @return boolean 是否有此人
     */
    @Override
    public boolean Login(String name, String passwd) {
        boolean flag = false;
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        Statement stmt = null;
        try {
//        获取数据库连接
            conn = jdbcUtils.ConnectMysql();
//        获取 Statement对象
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String sql = "select * from users where name='"+ name+"'";
//        String sql = "select * from users";
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        while(true){
            try {
                if (!rs.next()) break;
                int id = rs.getInt("id");
                String username = rs.getString("name");
                String userpass = rs.getString("password");
                String phone = rs.getString("phone");
                System.out.println("id: " + id + "\tname: "+ username + "\tpass: "+ userpass + "\tphone: "+phone+"\n");
                if(username.equals(name)){
                    if(userpass.equals(passwd)){
                        System.out.println("登录成功");
                        flag=true;
                    } else {
                            System.out.println("密码错误！！！");
                        }
                } else {
                    System.out.println("账号不存在!!!");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        jdbcUtils.release(rs,stmt,conn);
        return flag;
    }

    /**
     * 用户注册操作
     * @param name 用户名
     * @param passwd 密码
     * @param phone 电话号码
     */
    @Override
    public boolean Register(String name, String passwd, String phone) {
        boolean flag = false;
        JDBCUtils jdbcUtils = new JDBCUtils();
        Connection conn = null;
        Statement stmt = null;
        try {
//        获取数据库连接
            conn = jdbcUtils.ConnectMysql();
//        获取 Statement对象
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//        不能这么写，会导致 NullPointerException 空指针异常
//        if(name.equals("") || passwd.equals("") || phone.equals("")){
//        这样写防止空指针异常
//        System.out.println(name+passwd+phone);
//        System.out.println("".isEmpty());
//        System.out.println(("".equals(name)) && ("".equals(passwd)) && ("".equals(phone)));
        if(("".equals(name)) || ("".equals(passwd)) || ("".equals(phone))){
            System.out.println("输入信息不能为空！！！");
            jdbcUtils.release(stmt,conn);
            return flag;
        }else{
//            System.out.println("111");
            String sql = "insert into users (name,password,phone) values ("+ name + ","+ passwd + ","+ phone +");";
            try {
/*            在这里不能使用 会报错 executeQurery()是一个查询方法，而不能进行修改操作
            stmt.executeQuery(sql);
            execute()既能执行修改表中数据，又可以执行创建表，删除表，创建数据库，删除数据库等操作
            而excuteUpdate()针对于修改更新表中的数据，返回int类型的值，存储修改的行数
 */
                stmt.execute(sql);
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
            flag = true;
            jdbcUtils.release(stmt,conn);
        }
        return flag;
    }
}
